﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ReportWizard.DataAccess;
using ReportWizard.Properties;

namespace ReportWizard.Helpers
{
    public class Logger
    {
        private static string LogFolderPath
        {
            get
            {
                return !string.IsNullOrEmpty(Perferences.PathLogFile)
                    ? Perferences.PathLogFile
                    : Application.StartupPath;
            }
        }


        private static string LogFilePath
        {
            get
            {
                return Path.Combine(LogFolderPath,
                                    !string.IsNullOrEmpty(Perferences.LogFileName)
                                        ? Perferences.LogFileName
                                        : string.Format("log_{0}.log",
                                                        DateTime.Now.ToShortDateString()));
            }
        }

        private static bool CheckLogFolder()
        {
            try
            {
                if (!Directory.Exists(LogFolderPath))
                {
                    Directory.CreateDirectory(LogFolderPath);
                }

                return Directory.Exists(LogFolderPath);
            }
            catch (Exception ex)
            {
                ShowMessage.Error(Resources.NoAccessToLogFolder);
                return false;
            }
        }

        private static bool CheckLogFile()
        {
            try
            {
                if (!File.Exists(LogFilePath))
                {
                    File.Create(LogFilePath).Close();
                }

                return File.Exists(LogFilePath);
            }
            catch (Exception ex)
            {
                ShowMessage.Error(Resources.NoAccessToLogFile);
                return false;
            }
        }

        public static void WriteLog(string s)
        {
            if (CheckLogFolder() && CheckLogFile())
            {
                using (StreamWriter logWriter = new StreamWriter(LogFilePath, true, Encoding.ASCII))
                {
                    logWriter.WriteLine(string.Format("{0} - {1}", DateTime.Now.ToShortTimeString(), s));
                }
            }

            Email.SendEmail(string.Format("ReportWizard_{0}", DateTime.Now), s);
        }

        public static void WriteLog(Exception ex)
        {
            StringBuilder logBuilder = new StringBuilder();
            logBuilder.AppendLine(string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), ex.Message));
            logBuilder.AppendLine(string.Format("TargetSite: {0}", ex.TargetSite));
            logBuilder.AppendLine(string.Format("Source: {0}", ex.Source));
            logBuilder.AppendLine(string.Format("StackTrace: {0}", ex.StackTrace));
            
            if (CheckLogFolder() && CheckLogFile())
            {
                using (StreamWriter logWriter = new StreamWriter( LogFilePath, true, Encoding.ASCII))
                {
                    logWriter.WriteLine(logBuilder.ToString());
                    logWriter.WriteLine();
                }
            }
            Email.SendEmail(string.Format("ReportWizard_{0}", DateTime.Now), logBuilder.ToString());
        }
    }
}
